// 请求封装
const request = ({ url, data = {}, method = "GET", isThirdParty = false }) => {
  let header = {
    "Content-Type": "application/json",
    "Accept": "application/json",
  };
  const token = tt.getStorageSync("token")
  if (token) {
    header.Authorization = "Bearer " + token;
  }
  const { platform: XPlatform } = tt.getSystemInfoSync(true)
  if (XPlatform) {
    header["X-Platform"] = XPlatform
  }
  const app = getApp()
  const { appInfo, query, scene } = app.globalData
  const { appId, baseUrl } = appInfo
  return new Promise((resolve, reject) => {
    tt.request({
      url: isThirdParty ? url : baseUrl + url,
      method, header,
      data: { ...query, scene, appId, appid: appId, ...data },
      success(res) {
        if (res.statusCode == 200) {
          resolve(res.data);
        } else if (res.statusCode == 401) {
          tt.clearStorageSync()
          resolve(res.data);
        } else {
          tt.showToast({
            title: res.data.message,
            icon: 'none'
          });
          resolve(res.data);
        }
      },
      fail(err) {
        console.log('网络错误！', isThirdParty ? url : baseUrl + url);
        resolve({
          code: "",
          message: "网络错误！",
        });
      },
    });
  });
};

module.exports = { request };